<?php

namespace backend\services\common;

use backend\extensions\ApiBaseService;
// use backend\models\config\AdminRole;
// use backend\models\config\AdminUser;
use backend\models\goods\brand\GoodsBrand;
use backend\models\goods\cate\GoodsCate;
// use backend\models\merchant\Merchant;
// use backend\models\source\Source;
use backend\models\active\ActiveCoupon;
use common\models\active\ActivePromotionModel;
// use common\models\channel\ChannelModel;
// use common\models\coupon\StoreCouponLinkModel;
// use common\models\mall\MallAdModel;
// use common\models\member\MemberModel;
// use common\models\platform\dewu\DewuPriceModel;
// use common\models\promoter\PromoterCatesModel;

/**
 * 选项
 */
class OptionService extends ApiBaseService
{
    /**
     * 获取选项
     * @param array $keys
     * @return array
     */
    public static function getOptions($keys)
    {
        $result = [];
        foreach ($keys as $key) {
            if (!method_exists(self::class, $key)) {
                continue;
            }
            $result[$key] = self::{$key}();
        }

        return $result;
    }

    /**
     * 角色
     * @return array
     */
    // public static function roles()
    // {
    //     return AdminRole::getAll([], ['id', 'name']);
    // }

    /**
     * 商户
     * @return array
     */
    // public static function merchants()
    // {
    //     return Merchant::getAll([], ['id', 'name']);
    // }

    /**
     * 上游渠道
     * @return array
     * **/
    // public static function sources()
    // {
    //     return Source::getAll([], ['id', 'name']);
    // }

    /**
     * 品牌
     * @return array
     */
    public static function brands()
    {
        return GoodsBrand::find()->select(['id', 'name'])->asArray()->all();
    }

    /**
     * 分类
     * @return array
     */
    // public static function cates()
    // {
    //     return Cate::find()->select(['id', 'name'])->asArray()->all();
    // }
    /**
     * 分类
     * @return array
     */
    public static function cateTrees()
    {
        $list = GoodsCate::find()->select(['*', 'id value', 'name label'])->asArray()->all();
        
        return GoodsCate::arrayTree($list);
    }

    /**
     * 出价状态
     * @return array
     */
    // public static function dewuPriceStatus()
    // {
    //     $res = [];

    //     $status = DewuPriceModel::$state;
    //     foreach ($status as $key => $value) {
    //         $res[] = ['id' => (string)$key, 'name' => $value];
    //     }

    //     return $res;
    // }
    
    /**
     * 门店券（券平台）
     * @return array
     */
    // public static function storeCouponOptions()
    // {
    //     $list = StoreCoupon::find()
    //         ->where(['platform' => StoreCoupon::PLATFORM_COUPON])
    //         ->select(['id', 'name'])->orderBy(['id' => SORT_ASC])->asArray()->all();
    //     $result = [];
    //     foreach ($list as $item) {
    //         $result[] = [
    //             'value' => $item['id'],
    //             'label' => '【'. $item['id'] .'】'. $item['name'],
    //         ];
    //     }

    //     return $result;
    // }
    
    /**
     * 跨境券列表
     * @return array
     */
    public static function couponOptions()
    {
        $list = ActiveCoupon::find()
            ->select(['id', 'name'])->orderBy(['id' => SORT_ASC])->asArray()->all();
        $result = [];
        foreach ($list as $item) {
            $result[] = [
                'value' => $item['id'],
                'label' => '【'. $item['id'] .'】'. $item['name'],
            ];
        }

        return $result;
    }
    
    /**
     * 门店券组
     * @return array
     */
    // public static function storeCouponLinkOptions()
    // {
    //     return StoreCouponLinkModel::find()->select(['id value', 'name label'])->asArray()->all();
    // }
    
    /**
     * 渠道
     * @return array
     */
    // public static function channels()
    // {
    //     $admin = AdminUser::getDataByToken();
    //     $query = ChannelModel::find()->select(['id', 'name']);
    //     if (!empty($admin) && !empty($admin['channel'])) {
    //         $query->where(['id' => $admin['channel']]);
    //     }

    //     return $query->asArray()->all();
    // }
    
    /**
     * 推广员分类
     * @return array
     */
    // public static function promoterCates()
    // {
    //     $admin = AdminUser::getDataByToken();

    //     $query = PromoterCatesModel::find()->select(['id', 'name']);
    //     if (!empty($admin) && !empty($admin['channel'])) {
    //         $query->where(['channel' => $admin['channel']]);
    //     }
    //     return $query->asArray()->all();
    // }
    
    /**
     * 平台
     * @return array
     */
    // public static function sysPlatforms()
    // {
    //     $res = [];

    //     $platforms = MemberModel::$platform;
    //     foreach ($platforms as $key => $value) {
    //         $res[] = ['id' => (string)$key, 'name' => $value];
    //     }

    //     return $res;
    // }
    
   
    /**
     * 活动
     * @return array
     */
    public static function activeOptions()
    {
        $typeOpts = [];

        $types = ActivePromotionModel::$type;
        foreach ($types as $id => $name) {
            $typeOpts[] = ['id' => $id, 'name' => $name];
        }

        return [
            'type' => $typeOpts
        ];
    }
}
